####
# Author: M. Kenwick, S. Lee, B. Kolcak
# Purpose: Analysis of Electoral Outcomes
# Date: June 2, 2025 
####

rm(list=ls())
library(lme4)
library(dplyr)
library(ggplot2)
library(stargazer)
library(readxl)


setwd('~/Dropbox/cmr_cong/replication/elections/')

####kriner and shen replication data file 
subkriner <- read.csv("subkriner1.csv")
##variable descriptions
#fips: county district number 
#iraq_count_impute: county casualty count
#per_10000_iraq: county casualty rate/10000
#dkgscore: change in opponent quality
#dpctlngopexp: change in % gop spending
#pctbush2004: % Bush Vote 2004
#dunemp2005to2006: change in unemployment
#pct_military_age1864: 18-64 in Armed Forces
#pct_vets_all: % Veteran 
#changeingopinc: Change in GOP Senate Vote Share, 2000–2006 

####incumbent senators reelection datafile 
sen_auth <- read.csv("sen_auth.csv")
##variable descriptions
#voteshare: share of vote in reelection 2002/2004/2006
#votechange: share of vote 2002/2004/2006 - vote 1996/1998/2000 


####incumbent house of representatives reelection datafile (2004)
house_auth2004 <- read.csv("house_auth2004.csv")
#voteshare: share of vote in reelection 2004
#dist_fat_cum: district cumulative fatalities as of 2004 
#state_fat_cum: state cumulative fatalities as of 2004

####incumbent house of representatives reelection datafile (2006)
house_auth2006 <- read.csv("house_auth2006.csv")
#voteshare: share of vote in reelection 2006
#dist_fat_cum: district cumulative fatalities as of 2006
#state_fat_cum: state cumulative fatalities as of 2006

####incumbent house of representatives reelection datafile (2002)
house_auth2002 <- read.csv("house_auth2002.csv")


###############################################################################
# 1 - Table A32: Replication of Kriner and Shen (2007)
###############################################################################

mod1 <- lm(changeingopinc ~ iraq_count_impute + vet +  dkgscore + dpctlngopexp +  pctbush2004 + dunemp2005to2006 + pct_military_age1864 + pct_vets_all , data = subkriner)
mod2 <- lm(changeingopinc ~ iraq_count_impute*vet + dkgscore + dpctlngopexp +  pctbush2004 + dunemp2005to2006 + pct_military_age1864 + pct_vets_all , data = subkriner)
mod3 <- lm(changeingopinc ~ per_10000_iraq + vet +  dkgscore + dpctlngopexp +  pctbush2004 + dunemp2005to2006 + pct_military_age1864 + pct_vets_all , data = subkriner)
mod4 <- lm(changeingopinc ~ per_10000_iraq*vet + dkgscore + dpctlngopexp +  pctbush2004 + dunemp2005to2006 + pct_military_age1864 + pct_vets_all , data = subkriner)

stargazer(mod1, mod2, mod3, mod4, type = 'text')



###############################################################################
# 2 - Table 6: Incumbent Vote Share in Re-election Campaigns, 2002-2006
###############################################################################

sen_auth$sen <- 1
house_auth2004$elect_year <- 2004
house_auth2006$elect_year <- 2006
house_auth2004$sen <- 0
house_auth2006$sen <- 0
house_auth2002$elect_year <- 2002
house_auth2002$sen <- 0

data <- bind_rows(sen_auth,house_auth2004,house_auth2006, house_auth2002)

data$lncas <- NA
data$lncas[!is.na(data$fat_cum)] <- log(data$fat_cum[!is.na(data$fat_cum)]+.5)
data$lncas[!is.na(data$dist_fat_cum)] <- log(data$dist_fat_cum[!is.na(data$dist_fat_cum)]+.5)
data$cas2 <- NA
data$cas2[!is.na(data$fat_cum)] <- data$fat_cum[!is.na(data$fat_cum)]
data$cas2[!is.na(data$dist_fat_cum)] <- data$dist_fat_cum[!is.na(data$dist_fat_cum)]

t0 <- lm(voteshare ~ mil + rep + lncas  + sen + as.factor(elect_year), data = data)
t1 <- lm(voteshare ~ mil + rep + auth_yea + lncas + sen + as.factor(elect_year), data = data)
t2 <- lm(voteshare ~ mil + rep + auth_yea + lncas + sen + as.factor(elect_year), data = subset(data, rep==1))
t3 <- lm(voteshare ~ mil + rep + auth_yea + lncas + sen + as.factor(elect_year), data = subset(data, auth_yea==1))
t4 <- lm(voteshare ~ mil + rep + auth_yea + lncas + sen + as.factor(elect_year), data = subset(data, sen==1))
stargazer(t0, t1, t2, t3, t4, type = "text")


